package com.cyy.demo.test.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ThreadList {

    volatile List<Integer> list = new ArrayList<>();

     int count = 0;

     boolean isStop = false;

    public void add(){
        while (count<10){
           if(!isStop){
               list.add(1);
               count++;
               System.out.println("list.size()"+list.size());
               try {
                   TimeUnit.SECONDS.sleep(1);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }else {
               System.out.println(list.size());
               break;
           }
        }
    }

    public void size(){
       while (true){
           if(list.size()>=5){
               isStop = true;
               break;
           }
       }
    }


    public static void main(String[] args) {
        ThreadList t = new ThreadList();
        new Thread(t::add).start();
        new Thread(t::size).start();

    }
}
